part of base_package;

var logger = Logger();

extension StringUtil on String {
  /// toast提示框
  toast({
    // Duration(seconds: 2) 秒
    Duration? duration,
    // EasyLoadingToastPosition.center
    // top,center,bottom,
    EasyLoadingToastPosition? toastPosition,
  }) {
    EasyLoading.showToast(this,
        duration: duration, toastPosition: toastPosition);
  }

  /// 打印log
  log({String? tag, bool isShowTime = false}) {
    if (tag == null) {
      logger.i(isShowTime ? "[${TimeUtil.nowTime()}]--$this" : this);
    } else {
      logger.i(isShowTime
          ? "[${TimeUtil.nowTime()}]--[$tag]:$this"
          : "[$tag]:$this");
    }
  }

  /// md5 加密
  String get md5Encrypt {
    var content = const Utf8Encoder().convert(this);
    var digest = md5.convert(content);
    // 这里其实就是 digest.toString()
    return hex.encode(digest.bytes);
  }

  /// 切割小数位数
  String toStringAsFixed(int fractionDigits) {
    String valueString = this;
    String doubleString =
        valueString.substring(valueString.indexOf('.') + 1, valueString.length);
    String intString = valueString.substring(0, valueString.indexOf('.'));
    if (doubleString.length < fractionDigits) {
      String zero = '';
      for (int i = 0; i < (fractionDigits - doubleString.length); i++) {
        zero += '0';
      }
      return '$valueString$zero';
    } else {
      doubleString = doubleString.substring(0, fractionDigits);
      return '$intString.$doubleString';
    }
  }
}
